// pages/job/job.js
const app = getApp();
var page = 1;
var index = 5;
var city = 0;
var word = false;//区分搜索分页
Page({

  /**
   * 页面的初始数据
   */
  data: {
    areaCheck: '全部',
    pay: '结算方式',
    select: '分类方式',
    order: '排序',
    recommended: [],
    secshow: false,
    wappay: false,
    cover1isshow: false,
    cover2isshow: false,
    screen: ['不限'],
    code: '',
    navindex: 0,
    searchWords: '',
    nav: [
      { txt: '优质兼职' },
      { txt: '500强就业' },
      { txt: '出国就业' }
    ],
    allservices: [[], [], [],],
    types: [
      { type: '', type2: '' },
      { type: '', type2: '' },
      { type: '' }
    ],
    hidden: ''    //页面样式
  },

// 选项卡控制开始
  toshowc1: function (e) {
    var cover1isshow = !this.data.cover1isshow;
    this.setData({
      cover1isshow: cover1isshow,
      cover2isshow: false,
      cover3isshow: false,
      cover4isshow: false,
    })
  },
  toshowc2: function (e) {
    var cover2isshow = !this.data.cover2isshow;
    this.setData({
      cover2isshow: cover2isshow,
      cover1isshow: false,
      cover3isshow: false,
      cover4isshow: false,
    })
  },
  toshowc3: function (e) {
    var cover3isshow = !this.data.cover3isshow;
    this.setData({
      cover3isshow: cover3isshow,
      cover1isshow: false,
      cover2isshow: false,
      cover4isshow: false,
    })
  },
  toshowc4: function (e) {
    var cover4isshow = !this.data.cover4isshow;
    this.setData({
      cover4isshow: cover4isshow,
      cover1isshow: false,
      cover2isshow: false,
      cover3isshow: false,
    })
  },
  click: function (e) {
    var that = this;
    var index = e.currentTarget.dataset.index;
    var id = e.currentTarget.dataset.id;
    var txt = e.currentTarget.dataset.txt;
    var types = that.data.types
    types[index].type = txt
    this.setData({
      navindex: id,
      types: types
    })
    console.log(types)
  },
  waimask: function (e) {
    var index = e.currentTarget.id;
    var coverisshow = this.data.coverisshow;
    for (var i = 0; i < coverisshow.length; i++) {
      coverisshow[i] = false;
    }
    this.setData({
      coverisshow: coverisshow
    })
  },
  facus: function () {
    this.setData({
      cover1isshow: false,
      cover2isshow: false,
      cover3isshow: false,
      cover4isshow: false,
      secshow: false,
    });
  },
  tochangehp: function () {
    this.setData({
      cover1isshow: false,
      cover2isshow: false,
      cover3isshow: false,
      cover4isshow: false,
      secshow: false,
      searchWords: ''
    });
    page = 1;
    word = false;
    this.setData({
      recommended: []
    })
    this.getList();
  },
  //筛选
  checkArea: function (e) {
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      areaCheck: txt
    })
  },
  checkType: function (e) {
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      select: txt
    })
  },
  checkSort: function (e) {
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      order: txt
    })
  },
  checkPay: function (e) {
    let txt = e.currentTarget.dataset.txt;
    this.setData({
      pay: txt
    })
  },
  // 筛选完
  // 选项卡控制结束

  // 关键词搜索
  search: function (e) {
    let searchWords = this.data.searchWords;
    let that = this;
    let p = 1;
    that.setData({
      areaCheck: '全部',
      pay: '结算方式',
      select: '分类方式',
      order: '排序',
      recommended: []
    });
    that.getKeyList(searchWords);
  },
  searchWords: function (e) {
    this.setData({
      searchWords: e.detail.value
    })
  },

  //详情控制
  start_recommended: function (e) {
    // let cancel = e.currentTarget.dataset.cal;
    // if (!cancel) {
    //   wx.navigateTo({
    //     url: '../recommendedil/recommendedil?jobdeil=' + e.currentTarget.id,
    //   })
    // }
    let that = this;
    let cancel;
    let id = e.currentTarget.id;
    wx.request({
      url: app.data.Url + 'job/isCancel',
      data: { 'id': id },
      method: "post",
      success: function (res) {
        console.log(res);
        cancel = res.data;
        console.log(cancel, 'cancel');
        if (cancel == 0) {
          wx.navigateTo({
            url: '../recommendedil/recommendedil?jobdeil=' + id,
          })
        } else {
          wx.showToast({
            title: '已结束',
            duration: 1000,
            mask: true
          })
        }
      }
    })
  },
  //区域改变
  onShow: function(){
    let cityChange = wx.getStorageSync('cityChange');
    if (cityChange) {
      this.upDate();
      wx.setStorageSync('cityChange', false);
    }
  },

  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var openid = wx.getStorageSync('openid');
    var that = this;
    wx.request({
      //获取openid接口  
      url: 'https://www.135k.com/addons/sd_humans/tp/index.php/users/getvip',
      data: {
        uid: wx.getStorageSync('uid')
      },
      method: 'post',
      success: function (resid) {
        wx.setStorageSync('step', resid.data);
      }
    })
    let vip = wx.getStorageSync('step');
    if (vip == 0) {
      that.setData({
        'wappay': true,
        hidden:'hidden'
      })
    } 
      page = 1;
      that.setData({
        recommended: []
      })
    
    wx.request({
      url: 'https://www.135k.com/addons/sd_humans/tp/index.php/users/checkopenid',
      data: {
        openid: openid
      },
      method: "post",
      success: function (res) {
        wx.setStorageSync("uid", res.data);
        if (res.data == 0) {
          that.setData({
            wap: true
          })
        }
      }
    })
    that.upDate();    
  },
  /**
* 从服务器获取列表
*/
  getList: function () {
    let that = this;
    let city = that.data.city;
    let area = that.data.areaCheck;
    let pay = that.data.pay;
    let order = that.data.order;
    let select = that.data.select;
    wx.request({
      url: app.data.Url + 'screen/vsearch_list4',
      data: {
        screen: index,
        city: city,
        area: area,
        pay: pay,
        order: order,
        page: page,
        select: select
      },
      method: 'get',
      success: function (res) {
        console.log(res);
        if (res.data.data.length == 0) {
          wx.showToast({
            title: '已加载全部',
            icon: 'success',
            duration: 3000
          })
        }
        let recommended = that.data.recommended;
        for (var i = 0; i < res.data.data.length; i++) {
          recommended.push(res.data.data[i]);
        }
        page++;
        that.setData({
          recommended: recommended
        })
      }
    })
  },

  // 搜索关键字
  getKeyList(searchWords) {
    let that = this;
    word = true;
    wx.request({
      url: app.data.Url + 'screen/searchWords',
      data: {
        page: page,
        t: 2,
        search: searchWords
      },
      success: function (res) {
        if (res.data.data.length == 0) {
          wx.showToast({
            title: '已加载全部',
            icon: 'success',
            duration: 3000
          })
        }
        let recommended = that.data.recommended;
        for (var i = 0; i < res.data.data.length; i++) {
          recommended.push(res.data.data[i]);
        }
        page++;
        that.setData({
          recommended: recommended
        })
      }
    })
  },

  // 添加修改分类
  diqu: function () {
    wx.navigateTo({
      url: '../diqu/diqu',
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {
    var that = this;
    var uid = wx.getStorageSync("uid"); 
    if (uid != '') {
    } else {
      wx.request({
        url: 'https://www.135k.com/addons/sd_humans/tp/index.php/users/userinfo',
        data: {
          uid: uid
        },
        method: "post",
        success: function (res) {
          wx.setStorageSync("usid", res.data[0].uid);
        }
      })
    }    
    // 获取用户基本信息
    wx.login({
      success: function (resss) {
        that.setData({
          code: resss.code
        })
      }
    })
  },

  /**
   * 生命周期函数--监听页面显示
   */
  upDate: function () {
    let that = this;
    that.setData({
      areaCheck: '全部',
      pay: '结算方式',
      select: '分类方式',
      order: '排序',
      recommended: []
    });
    page = 1;
    // 获取当前城市
    let city = wx.getStorageSync('city');
    this.setData({
      city: city
    });
    // 获取当前城市下区域列表
    let areas = wx.request({
      url: app.data.Url + 'job/vareas',
      data: { 'city': city },
      success: function (res) {
        console.log(res);
        that.setData({
          area: res.data[0],
          allservices: res.data[1],
          recommended: []
        })
        that.getList();
      }
    })
  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {
  
  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {
  
  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {
    wx.stopPullDownRefresh()
  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {
    let that = this;
    wx.showLoading({
      title: '拼命加载中……',
      duration: 1000
    })
    if (!word) {
      that.getList();
    } else {
      let searchWords = wx.getStorageInfoSync('searchWords');
      that.getKeyList();
    }
  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {
  
  },
  buy: function(){
    wx.navigateTo({
      url: "../membuy/membuy"
    })
  }
})